package hot_100;

public class 合并两个有序链表 {
    public static void main(String[] args) {

    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode ans = new ListNode();
        ListNode net = ans;
        ListNode temp;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                temp = new ListNode(list1.val);
                list1 = list1.next;
            } else {
                temp = new ListNode(list1.val);
                list2 = list2.next;
            }
            net.next = temp;
            net = temp;
        }
        while (list1 != null) {
            temp = new ListNode(list1.val);
            list1 = list1.next;
            net.next = temp;
            net = temp;
        }
        while (list2 != null) {
            temp = new ListNode(list2.val);
            list2 = list2.next;
            net.next = temp;
            net = temp;
        }
        return ans.next;
    }

    public class ListNode {
        int val;
        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
